﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Reports.Cinema
{
    public class Report1 : DependencyObject
    {
        public static DependencyProperty StartDateProperty = DependencyProperty.Register("StartDate", typeof(DateTime), typeof(Report1));
        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set
            {
                SetValue(StartDateProperty, value);
            }
        }
        public static DependencyProperty EndDateProperty = DependencyProperty.Register("EndDate", typeof(DateTime), typeof(Report1));
        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set
            {
                SetValue(EndDateProperty, value);
            }
        }
        public Report1()
        {
            EndDate = DateTime.Now;
            if (DateTime.Now.Month > 1)
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, DateTime.Now.Day);
            else StartDate = new DateTime(DateTime.Now.Year - 1, 12, DateTime.Now.Day);
        }
        public CrystalReport1 Load(TinyCinema.Bussiness.Login.Employee LoginInfo)
        {
            string sql = "select NgayHD,DoanhSoVe, case DoanhSoTD when null then 0 else DoanhSoTD end as DoanhSoTD, (DoanhSoTD+DoanhSoVe) as Tong from ( select NgayHD, case DoanhSoVe when null then 0 else DoanhSoVe end as DoanhSoVe from ( select NgayHDTD as NgayHD from HOADONTHUCDON"
                + " where NgayHDTD >='" + StartDate + "'"
                + " and NgayHDTD <='" + EndDate + "'"
                + " group by NgayHDTD union select NgayHDV from HOADONVE"
                + " where NgayHDV >='" + StartDate + "'"
                + " and NgayHDV <='"+EndDate+"'"
                + " group by NgayHDV ) as Table1 left join (select NgayHDV,sum(HOADONVE.TriGia) as DoanhSoVe from HOADONVE group by NgayHDV) as Table2 on Table1.NgayHD = Table2.NgayHDV) as Table3 left join (select NgayHDTD,sum(HOADONTHUCDON.TriGia) as DoanhSoTD from HOADONTHUCDON group by NgayHDTD) as Table4 on Table3.NgayHD = Table4.NgayHDTD";
            CrystalReport1 report = new CrystalReport1();
            report.SetDataSource(TinyCinema.Data.Execution.Excute(sql));
            report.SetParameterValue("StartDate", StartDate);
            report.SetParameterValue("EndDate", EndDate);
            report.SetParameterValue("EmployeeName", LoginInfo.Emp.Name);
            return report;
        }
    }
}
